#include "volumetool.h"
#include "ui_volumetool.h"
#include<QGraphicsDropShadowEffect>
#include<QPainter>
#include<QDebug>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false),
    volumeRatio(20)
{
    ui->setupUi(this);

    //设置窗口为模态的,让这个窗口无边界
    setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    //给窗口添加阴影 通用步骤  1:创建QGraphicsDropShadowEffect对象 2:设置偏移量 设置颜色 设置圆角效果
    QGraphicsDropShadowEffect* shadoweffct=new QGraphicsDropShadowEffect(this);
    shadoweffct->setOffset(0,0);
    shadoweffct->setColor("#646464");
    shadoweffct->setBlurRadius(10);
    this->setGraphicsEffect(shadoweffct);

    ui->volumeRatio->setText("%20");
    ui->outSlider->setGeometry(ui->outSlider->x(), 180 - 36 + 25, ui->outSlider->width(), 36);
    ui->sliderBtn->move(ui->sliderBtn->x(), ui->outSlider->y() - ui->sliderBtn->height()/2);
   ui->silenceBtn->setIcon(QIcon(":/image/silence.png"));
     //建立关系
     connect(ui->silenceBtn,&QPushButton::clicked,this,&VolumeTool::OnSilenceBtnClicked);
     ui->volumeBox->installEventFilter(this);

}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::setVolume()
{
    //获取鼠标的y坐标,
    int height=ui->volumeBox->mapFromGlobal(QCursor().pos()).y();
    //判断鼠标的位置,不能越界
    height=height<25?25:height;
    height=height>205?205:height;

    //修改outline的高度
    ui->outSlider->setGeometry(ui->outSlider->x(),height,ui->outSlider->width(),205-height);
    //修改silderbtn圆的高度
    ui->sliderBtn->move(ui->sliderBtn->x(),height-ui->sliderBtn->height()/2);

    //计算volumeradio的值
    volumeRatio=(int)((ui->outSlider->height()/(float)180)*100);
    ui->volumeRatio->setText("%"+QString::number(volumeRatio));
}

//事件重绘,当窗口显示,或被遮挡再次显示都会发生重绘事件
void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void)event;

    //自定义控件的形状(三角形)

    //1:创建QPainter的对象,并且指定画笔在那个设备上进行绘画
    QPainter painter(this);

    //设置抗锯齿
    painter.setRenderHint(QPainter::Antialiasing, true);

    //2:给painter对象设置画笔
    painter.setPen(Qt::NoPen);

    //设置画刷颜色
    painter.setBrush(Qt::white);

    QPolygon polygon;
    polygon.append(QPoint(30,300));
    polygon.append(QPoint(70,300));
    polygon.append(QPoint(50,320));
    painter.drawPolygon(polygon);

}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
   //检测的对象是volumeBox
   if(watched==ui->volumeBox)
   {
       //鼠标点击事件
       if(event->type()==QEvent::MouseButtonPress)
       {
           //计算outline的高度与改变silence的高度
           setVolume();
       }
       //鼠标释放
       else if(event->type()==QEvent::MouseButtonRelease)
       {
           //通过信号将音量大小传递给qqmusic中
           emit setMusicVolume(volumeRatio);
       }
       else if(event->type()==QEvent::MouseMove)
       {
           setVolume();
           emit setMusicVolume(volumeRatio);
       }
       return true;
   }
   //交给父类对象来处理
   return QObject::eventFilter(watched,event);
}

void VolumeTool::OnSilenceBtnClicked()
{
   isMuted=!isMuted;
   if(isMuted==true)
   {
       //告诉播放器将歌曲设置为静音
       //先设置图标
       qDebug()<<"11111";
       ui->silenceBtn->setIcon(QIcon(":/image/silence.png"));

   }
   else
   {
         //qDebug()<<"11111";
       ui->silenceBtn->setIcon(QIcon(":/image/sound.png"));
   }
   emit setIsMuted(isMuted);

}
